function [result, Ir ] = character_Segmentation123( img, IS_DEBUG, FOLDER_RESULT,file_name, mode )
%CHARACTER_SEGMENTATION Summary of this function goes here
%   Detailed explanation goes here

z = size(img,3);
if z==3
    img = rgb2gray(img);
end

maxwidth = 256;
img=imresize(img,[NaN maxwidth]);
if mode == 2
    xmin= 5;
    ymin= 0;
    [maxhigh, maxwidth] = size(img);
    width = maxwidth - 2*xmin;
    high = maxhigh-2*ymin;
    img = imcrop(img, [xmin ymin width high]);
elseif mode ==3
    xmin= 0;
    ymin= 5;
    [maxhigh, maxwidth] = size(img);
    width = maxwidth - 2*xmin;
    high = maxhigh-2*ymin;
    img = imcrop(img, [xmin ymin width high]);
end

% [Gmag, ~] = imgradient(img,'prewitt');

im1= medfilt2(img,[3 3]); %Median filtering the image to remove noise%
BW = edge(im1,'canny'); %finding edges
BW = bwmorph(BW,'bridge');
% BW = imdilate(BW);
msk=[0 0 0 0 ;
    0 1 1 0 ;
    0 1 1 0 ;
    0 0 0 0 ];
B=conv2(double(BW),double(msk),'same');
% B = removeEdge(B);

im2 = imfill(B,'hole');

SE = ones(1,5);
im2 = imopen(im2,SE);
SE = ones(5,1);
im2 = imopen(im2,SE);
SE = ones(1,5);
im2 = imopen(im2,SE);
im2 = bwareaopen(im2, 200);
Ir = im2;
Ib = Sauvola(img,[0.1 128 15]);
Ib = bwareaopen(~Ib, 200);
cost = 10;
im2_ivn = im2';
[~, vector] = characterSegment.breakCost(im2_ivn, cost);

[ arrLineF, arrLineS] = characterSegment.getLine(vector>cost, Ib', im2_ivn);
result = {};
numResult = 0;

for iL = 1:length(arrLineF)
    aSegment = arrLineF{iL};
    
    cost = 3;
    aSegment = bwareaopen(aSegment, 200);
    [aSegment , ~] = characterSegment.breakCost(aSegment, cost);
    [arrCharacterF, arrCharacterS] = characterSegment.getCharacter(aSegment, arrLineS{iL}, 0 );
    [hSegment,wSegment] = size(aSegment);
    for i=1: length(arrCharacterF)
        subimg1 = (arrCharacterF{i});
        subimg2 = (arrCharacterS{i});
        ftemp = imfill(subimg2,'hole');
        ftemp = xor(ftemp,subimg2);
        subimg = xor(subimg1, ftemp);
        
        %         CC1 = bwconncomp(subimg,8);
        %         featureData = regionprops(CC1,'Orientation');
        %         oriented = featureData(1).Orientation;
        %         if oriented<0
        %            oriented = oriented+180;
        %         end
        %         subimg = imrotate(subimg,90-oriented);
        %         if oriented<0
        %             oriented = -oriented;
        %         end
        %         tempsubimg = imshear(subimg,oriented-90);
        
        v1 = sum(subimg,1);
        v2 = (sum(subimg,2))';
        x1 = find(v1>3,1,'first');
        x2 = find(v1>3,1,'last');
        y1 = find(v2>3,1,'first');
        y2 = find(v2>3,1,'last');
        w = abs(x2-x1);
        h = abs(y2-y1);
        % x y w h
        if wSegment/hSegment > 1.5
            ratio = 0.5;
        else
            ratio = 0.3;
        end
        if (w > 5) && (w < (0.3*wSegment)) && (h > (ratio*hSegment))
            if(max(v1) <1 || max(v2)<1)
                subimg = subimg2;
            else
                subimg = imcrop(subimg,[x1 y1 (x2-x1-1) (y2-y1-1)]);
            end
            numResult = numResult +1;
            result{numResult}= subimg;
        end
        
    end
    
    if IS_DEBUG
        numCharacter = length(result);
        col = ceil(sqrt(numCharacter));
        row = ceil(numCharacter/col);
        figure(2);
        for i=1:numCharacter
            subplot(row, col, i);
            imshow(result{i});
        end
        
    end
    
end
end

